<?php

/**
 * Manage actions which relate to send mail
 * 
 * @author Tuan
 */
class MailHelper {

    const EMAIL_ADMIN = 'traveldonal@gmail.com';
    const NAME_ADMIN = 'Travel Donald';

    /**
     * send mail
     * 
     * @param type $subject
     * @param type $body
     * @param type $receipt
     * @param type $recipientName
     * @param type $emailSender
     * @param type $nameSender
     * @param type $attachmentUrl     
     */
    public static function send($subject, $body = '', $receiptEmail = '', $recipientName = '', $emailSender = '', $nameSender = '', $attachmentUrl = '') {
        require_once ROOT_PATH . '/extension/phpmailer/class.phpmailer.php';


        $message = '
		    <html>
		    <head>
		      <title>' . $subject . '</title>
		    </head>
		    <body>
		      ' . $body . '
		    </body>
		    </html>
		    ';

        $mail = new PHPMailer();

        $mail->From = $emailSender;
        $mail->FromName = $nameSender;
        $mail->AddAddress($receiptEmail);

        $mail->WordWrap = 50; // set word wrap
        $mail->Priority = 1;

        $mail->Subject = $subject;
        $mail->Body = $message;
        $mail->IsHTML(true);
        if ($attachmentUrl) {
            $mail->AddAttachment($attachmentUrl);
        }
		
        return $mail->Send();
    }

    /**
     * when admin send invoice to customer      
     * @author :Tuan
     * * */
    public static function sendInvoice($idRequest, $emailReceive, $nameReceive,
	    $aStartLocation, $aEndLocation, $aStartTime, $aEndTime, $adult,
		$children, $infant, $class, $country,$price) {        
        $emailTemplate = Emailtemplate::ListEmailTempByIdEmailTemp(2); // chon temp email=1
		
	$adminEmail = get_site_option('ahed_email');        
        $adminName = getUserNameByEmail($adminEmail);	   
	$infoInvoice = '';	
	
	$infoInvoice .= '<table border="1" cellpadding="4">';
	for($i=0; $i < count($aStartLocation); $i++){
	    $infoInvoice .= '<tr>';
	    $infoInvoice .= '<td>Leaving from</td>';
	    $infoInvoice .= '<td>'.$aStartLocation[$i].'</td>';
	    $infoInvoice .= '</tr>';
	    $infoInvoice .= '<tr>';
	    $infoInvoice .= '<td>Going to</td>';
	    $infoInvoice .= '<td>'.$aEndLocation[$i].'</td>';
	    $infoInvoice .= '</tr>';
	    $infoInvoice .= '<tr>';
	    $infoInvoice .= '<td>Departure date</td>';
	    $infoInvoice .= '<td>'.$aStartTime[$i].'</td>';
	    $infoInvoice .= '</tr>';
	    $infoInvoice .= '<tr>';
	    $infoInvoice .= '<td>Return date</td>';
	    $infoInvoice .= '<td>'.$aEndTime[$i].'</td>';
	    $infoInvoice .= '</tr>';
	    $infoInvoice .= '<tr>';
	    $infoInvoice .= '<td colspan="2"></td>';	    
	    $infoInvoice .= '</tr>';
	}	
	$infoInvoice .= '<tr>';
	$infoInvoice .= '<td>Adult</td>';
	$infoInvoice .= '<td>'.$adult.'</td>';
	$infoInvoice .= '</tr>';
	$infoInvoice .= '<tr>';
	$infoInvoice .= '<td>Children</td>';
	$infoInvoice .= '<td>'.$children.'</td>';
	$infoInvoice .= '</tr>';
	$infoInvoice .= '<tr>';
	$infoInvoice .= '<td>Infant</td>';
	$infoInvoice .= '<td>'.$infant.'</td>';
	$infoInvoice .= '</tr>';
	$infoInvoice .= '<tr>';
	$infoInvoice .= '<td>Class</td>';
	$infoInvoice .= '<td>'.$class.'</td>';
	$infoInvoice .= '</tr>';
	$infoInvoice .= '<tr>';
	$infoInvoice .= '<td>Country</td>';
	$infoInvoice .= '<td>'.$country.'</td>';
	$infoInvoice .= '</tr>';
	
	$infoInvoice .= '<tr>';
	$infoInvoice .= '<td><strong>Price</strong></td>';
	$infoInvoice .= '<td><span style="color: red;">'.$price.'</span></td>';
	$infoInvoice .= '</tr>';
	
	$infoInvoice .= '</table>';
	$paypalBtn = '<a href="'.get_permalink(105).'?i='.$idRequest.'"><img src="'.  get_bloginfo('template_directory').'/images/paypal-icon.png"/></a>';
		
	$body = str_replace(
		array('[NAME]', '[INFO_INVOICE]', '[PAYPAL_BUTTON]'), array(
				'Tuan', $infoInvoice, $paypalBtn					
			), $emailTemplate[0]->content
	);

		
	self::send($emailTemplate[0]->subject, $body, $emailReceive, $nameReceive, $adminEmail, $adminName);        
    }
}
